import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

//课程的页面
import Course from "@/views/course/Course.vue";
import CourseInfo from "@/views/course/CourseInfo.vue";
import CoursePlay from "@/views/course/CoursePlay.vue";
//会员页面
import Member from "@/views/Member.vue";
//购物车
import Cart from "@/views/Cart/Cart";
// 结算页面
import ConfirmOrder from "@/views/Cart/ConfirmOrder";
// 我的课程
import About from "@/views/about/About.vue";
import MyCourse from "@/views/about/components/MyCourse";
import Collean from "@/views/about/components/Collean";
// 个人中心
import User from "@/views/user/user.vue";
import Setbindsns from '@/views/user/components/Setbindsns.vue'
import Setavatar from '@/views/user/components/Setavatar.vue'


Vue.use(VueRouter);

const routes = [
  { path: "/", name: "home", component: Home },
  { path: "/course", name: "course", component: Course },
  { path: "/course-info/:courseId", name: "courseinfo", component: CourseInfo },
  {
    path: "/course-play/:courseId/:chapterId",
    name: "courseplay",
    component: CoursePlay,
  },
  { path: "/Member", name: "Member", component: Member },
  { path: "/Cart", name: "Cart", component: Cart },
  { path: "/confirmOrder", name: "confirmOrder", component: ConfirmOrder },
  // 我的课程路由
  {
    path: "/about",
    name: "about",
    component: About,
    reditect: "/about/my-course",
    children: [
      {
        path: "my-course",
        name: "my-course",
        component: MyCourse,
      },
      {
        path: "collean",
        name: "collean",
        component: Collean,
      },
    ],
  },
  // 个人中心
  {
    path: "/user",
    name: "user",
    component: User,
    reditect: "/user/setbindsns",
    meta: { requiresAuth: true },
    children: [
      {
        path: "setbindsns",
        name: "setbindsns",
        component: Setbindsns,
      },
      {
        path: 'setavator',
        name: 'setavator',
        meta: { requiresAuth: true },
        component: Setavatar
      }
    ],
  },


];




const router = new VueRouter({
  routes,
})


//导航守卫    给要保护的路由加上meta: { requiresAuth: true },
router.beforeEach((to,from,next) => {
  let isLogin = localStorage.getItem('isLogin')
  if(to.matched.some(rec => rec.meta.requiresAuth)){  // 如果requiresAuth有    
    if(!isLogin){  // 没有token不能访问
      //没有登录
      console.log('未登录')
    }else{
      next()  //不需要token的 放行
    }
  }else{
    next()   // 不需要requiresAuth放行
  }
})

export default router;
